package com.tencent.org.service.impl;

import com.tencent.basic.exception.BusinessException;
import com.tencent.basic.service.serviceimpl.BaseServiceImpl;
import com.tencent.basic.util.BaiduAuditUtils;
import com.tencent.basic.util.Md5Utils;
import com.tencent.basic.util.StrUtils;
import com.tencent.org.domain.Emp;
import com.tencent.org.domain.Shop;
import com.tencent.org.domain.ShopAuditLog;
import com.tencent.org.dto.ShopDto;
import com.tencent.org.mapper.EmpMapper;
import com.tencent.org.mapper.ShopAuditLogMapper;
import com.tencent.org.mapper.ShopMapper;
import com.tencent.org.service.IShopService;
import com.tencent.user.domain.Logininfo;
import com.tencent.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 1.校验值是否为空
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店名不能为空");
        }
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        // 说明在数库中找到了一样的名字和地址，该店已经注册了
        if (dbShop != null){
            throw new BusinessException("该店已经注册");
        }

        // 百度智能审核  文本
        Boolean boo1 = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());
        if (!boo1){
            throw new BusinessException("店铺名称和地址违规");
        }

        // 百度智能审核  图片
        String url = "http://123.207.27.208" + shop.getLogo();
        Boolean boo2 = BaiduAuditUtils.ImgCensor(url);
        if (!boo2){
            throw new BusinessException("店铺Logo违规");
        }


        // 2添加管理员
        Emp admin = shop.getAdmin();
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);

        admin.setLogininfo_id(logininfo.getId());
        empMapper.add(admin);

        // 3.将自增id设置到t_shop的admin_id中
        shop.setAdminId(admin.getId());

        // 4.添加shop
        shopMapper.add(shop);

        // 5.将shop_id更新到t_employee表中
        admin.setShop_id(shop.getId());
        empMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态

        // 通过方法找到要审核的shop         找到要审核的店铺id
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        // 修改审核状态
        shop.setState(2);
        // 修改店铺
        shopMapper.update(shop);

        // 2.添加审核日志

        // 修改日志表状态
        shopAuditLog.setState(2);
        // 设置管理员id
        shopAuditLog.setAuditId(350L);
        // 保存审核日志
        shopAuditLogMapper.add(shopAuditLog);

        // 3.发送邮件激活id

        // 创建一个复杂邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        // 传入的信息， 可以带附件， 编码格式
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        // 发件人
        helper.setFrom("389616765@qq.com");
        // 找到店长
        Emp emp = empMapper.findById(shop.getAdminId());
        // 收件人
        helper.setTo(emp.getEmail());
        // 邮件标题
        helper.setSubject("店铺入驻成功激活");
        // 邮件内容
        helper.setText("<h1>店铺激活邮件!!!</h1><h3><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShopId()
                +"'>点击该链接激活店铺</a></h3>",true);
        // 发送邮件
        javaMailSender.send(message);
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态

        // 通过方法找到要审核的shop         找到要审核的店铺id
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());
        // 修改审核状态
        shop.setState(4);
        // 修改店铺
        shopMapper.update(shop);

        // 2.添加审核日志

        // 修改审核状态
        shopAuditLog.setState(4);
        // 设置管理员id
        shopAuditLog.setAuditId(350L);
        // 保存审核日志
        shopAuditLogMapper.add(shopAuditLog);

        // 3.发送邮件激活id

        // 创建一个复杂邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        // 传入的信息， 可以带附件， 编码格式
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        // 发件人
        helper.setFrom("389616765@qq.com");
        // 找到店长
        Emp emp = empMapper.findById(shop.getAdminId());
        // 收件人
        helper.setTo(emp.getEmail());
        // 邮件标题
        helper.setSubject("店铺入驻激活失败");
        // 邮件内容
        helper.setText("<h1>店铺激活失败!!!</h1><h3><a href='http://localhost:8081/#/register'>点击该链接重新激活店铺</a></h3>",true);
        // 发送邮件
        javaMailSender.send(message);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
